package com.secondshop.controllers;

import com.alipay.api.AlipayApiException;
import com.secondshop.models.Cart;
import com.secondshop.models.Item;
import com.secondshop.models.Order;
import com.secondshop.models.User;
import com.secondshop.services.*;
import com.secondshop.utils.Alipay;
import com.secondshop.utils.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PayController {
    private final GoodService goodService;
    private final TypeService typeService;
    private final ReviewService reviewService;
    private final UserService userService;
    private final ImageService imageService;
    private final CollectService collectService;
    private final OrderService orderService;

    @Autowired
    public PayController(GoodService goodService, TypeService typeService, ReviewService reviewService, UserService userService, ImageService imageService, CollectService collectService,OrderService orderService) {
        this.goodService = goodService;
        this.typeService = typeService;
        this.reviewService = reviewService;
        this.userService = userService;
        this.imageService = imageService;
        this.collectService = collectService;
        this.orderService=orderService;
    }
    Alipay alipay=new Alipay();
    @RequestMapping(value = "/pay/erweima", method = RequestMethod.GET)
    public String getErWeiMa(HttpServletRequest request , ModelMap model){
        String erweima=null;
        RandomString randomString=new RandomString();
        String out_trade_no=randomString.getRandomString2();
        ServletContext context=request.getServletContext();
        context.setAttribute("out_trade_no",out_trade_no);
        //从session获取购物车对象
        HttpSession session=request.getSession();
        Cart cart=(Cart) session.getAttribute("cart");
        Float price=cart.getTotalMoney();
        erweima=alipay.QRCode(price,out_trade_no);
        model.addAttribute("erweima",erweima);
        return "home/erweima";

    }



    @RequestMapping(value = "/pay/payQuery" ,method = RequestMethod.GET)
    public String payQuery(ModelMap model ,HttpServletRequest request){
        String msg="Success";
        ServletContext context=request.getServletContext();
       String out_trade_no= (String) context.getAttribute("out_trade_no");

        model.addAttribute("msg",alipay.payQuery(out_trade_no));
        if (msg.equals(alipay.payQuery(out_trade_no))){
            return "redirect:/pay/payEnd" ;
        }
        return "home/payError";

    }
    @RequestMapping(value = "/pay/payEnd" ,method = RequestMethod.GET)
    public  String payEnd(HttpServletRequest request){
        ServletContext context=request.getServletContext();
        List<Order> orders = (List<Order>) context.getAttribute("orders");
        for (Order order:orders){
            orderService.updateStatus(1,order.getId());
        }

        return "redirect:/user/userProfile";

    }

}
